package com.lzj.weather02;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

/**
 * "102453","广东","佛山市","440600","晴","20","南","≤3","73","20/10/2020 08:52:19","20/10/2020 09:00:01"
 * "102454","广东","佛山市","440600","晴","23","南","≤3","73","20/10/2020 08:53:19","20/10/2020 09:01:01"
 */
public class Weather implements WritableComparable<Weather> {

    private String adcode;
    private Integer year;
    private Integer month;
    private Integer day;
    private Integer temperature;
    private String province;
    private String city;

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(this.adcode);
        out.writeInt(this.year);
        out.writeInt(this.month);
        out.writeInt(this.day);
        out.writeInt(this.temperature);
        out.writeUTF(this.province);
        out.writeUTF(this.city);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.adcode = in.readUTF();
        this.year = in.readInt();
        this.month = in.readInt();
        this.day = in.readInt();
        this.temperature = in.readInt();
        this.province = in.readUTF();
        this.city = in.readUTF();
    }

    @Override
    public int compareTo(Weather o) {
        //首先比较区划编号
        int result = this.adcode.compareTo(o.getAdcode());
        //查看比较结果，如果区划编码相同继续比较，如果不同，直接返回结果
        if (result == 0) {
            //默认比较年
            result = this.year.compareTo(o.getYear());
            //查看比较结果，如果年相同继续比较，如果年不同，直接返回结果
            if (result == 0) {
                //开始比较月份
                result = this.month.compareTo(o.getMonth());
                //查看比较结果，如果月相同继续比较温度，如果月份不同，直接返回结果
                if (result == 0) {
                    //开始比较温度
                    result = this.temperature.compareTo(o.getTemperature()) * -1;
                    if (result == 0) {
                        //开始比较天
                        result = this.day.compareTo(o.getDay());
                    }
                }
            }
        }
        return result;
    }

    public Weather() {
    }

    public Weather(String adcode, Integer year, Integer month, Integer day, Integer temperature, String province, String city) {
        this.adcode = adcode;
        this.year = year;
        this.month = month;
        this.day = day;
        this.temperature = temperature;
        this.province = province;
        this.city = city;
    }

    public String getAdcode() {
        return adcode;
    }

    public void setAdcode(String adcode) {
        this.adcode = adcode;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public Integer getDay() {
        return day;
    }

    public void setDay(Integer day) {
        this.day = day;
    }

    public Integer getTemperature() {
        return temperature;
    }

    public void setTemperature(Integer temperature) {
        this.temperature = temperature;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Weather weather = (Weather) o;
        return Objects.equals(adcode, weather.adcode) &&
                Objects.equals(year, weather.year) &&
                Objects.equals(month, weather.month) &&
                Objects.equals(day, weather.day) &&
                Objects.equals(temperature, weather.temperature) &&
                Objects.equals(province, weather.province) &&
                Objects.equals(city, weather.city);
    }

    @Override
    public int hashCode() {
        return Objects.hash(adcode, year, month, day, temperature, province, city);
    }

    @Override
    public String toString() {
        return "Weather{" +
                "adcode='" + adcode + '\'' +
                ", year=" + year +
                ", month=" + month +
                ", day=" + day +
                ", temperature=" + temperature +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}